# Maintainer: Peter Budai <peterbud@hotmail.com>

_realname=openlibm
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.7.1
pkgrel=1
pkgdesc="High quality system independent, portable, open source libm implementation (mingw-w64)"
arch=('any')
url='http://www.openlibm.org'
license=('MIT')
options=('strip' 'staticlibs')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/JuliaMath/${_realname}/archive/v${pkgver}.tar.gz")
sha256sums=('d4b252b71c74571fe5b39d42dee03ccca4bb238827e6a2c9ba108dbca2d3e879')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  sed -i "s|CFLAGS_add += -nodefaultlibs|#CFLAGS_add += -nodefaultlibs|" Make.inc

  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  cp -rf "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${CARCH}"
}

build() {
  cd "${srcdir}/build-${CARCH}"
  
  make prefix=${MINGW_PREFIX} USEGCC=1
}

check() {
  cd "${srcdir}"/build-${CARCH}
  make check || true
}

package() {
  cd "${srcdir}"/build-${CARCH}
  make  prefix=${MINGW_PREFIX}  DESTDIR="${pkgdir}" install

  install -Dm644 ${srcdir}/build-${CARCH}/LICENSE.md ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
